﻿using DevExpress.XtraEditors;
using DevExpress.XtraEditors.ViewInfo;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using Lamex.Base;
using Lamex.Common;
using Lamex.NC.BLL;
using Lamex.NC.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Lamex.mNC.UI
{
    
    public partial class frmNC_StaffPlan : Form
    {
        string accoutId = "";
        public frmNC_StaffPlan()
        {
            InitializeComponent();
            this.Load += new EventHandler(frmNC_StaffPlan_Load);
            this.gridView1.CellValueChanged += new DevExpress.XtraGrid.Views.Base.CellValueChangedEventHandler(this.gridView1_CellValueChanged);
            
        }
        int iFocusRow = 0;
        private void gridView1_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (e.Column.FieldName == "Account")
            {
                int m = e.RowHandle;
                
                GridView view = sender as GridView;
                string cellData = "";
                if (view.GetRowCellDisplayText(m, "Account") == null)
                    cellData = "";
                else
                    cellData = gridView1.GetRowCellDisplayText(m, "Account").ToString();
                if (string.IsNullOrEmpty(cellData)) return;

                DataTable dt = BaseBuildSql.GetDataTableBySql("select * from Sys_MyUser where  Account='" + cellData + "'");
                if (dt.Rows.Count != 0)
                {
                    DataTable dt1 = BaseBuildSql.GetDataTableBySql("select * from NC_Convert_StaffPlan where  Account='" + cellData + "'");
                    if(dt1.Rows.Count==0)
                        gridView1.SetRowCellValue(m, "UserName", dt.Rows[0]["UserName"].ToString());
                    else
                    {
                        XtraMessageBox.Show("分配人员已存在请核实！");
                        gridView1.SetRowCellValue(m, "UserName", "");
                    }
                }
                else
                {
                    XtraMessageBox.Show("不存在此人员帐号！");
                }

                //DataTable dt2 = BaseBuildSql.GetDataTableBySql("select * from NC_Convert_StaffPlan where  Account='" + cellData + "'");
                //if (dt2.Rows.Count != 0)
                //{
                //    gridView1.SetRowCellValue(m, "UserName", dt.Rows[0]["UserName"].ToString());
                //}
                //else
                //{
                //    XtraMessageBox.Show("不存在此人员帐号！");
                //}
            }
            if (e.Column.FieldName == "Coefficient")
            {
                int m = e.RowHandle;

                GridView view = sender as GridView;
                string cellData = "";
                if (view.GetRowCellDisplayText(m, "Coefficient") == null)
                    cellData = "";
                else
                    cellData = gridView1.GetRowCellDisplayText(m, "Coefficient").ToString();
                if (string.IsNullOrEmpty(cellData)) return;
                decimal value = decimal.Parse(cellData);
                if(!(0<= value &&  value<=1))
                {
                    XtraMessageBox.Show("系数不在存0-1范围内");
                    gridView1.SetRowCellValue(m, "Coefficient","");
                }
                   

                
            }
        }
        private void frmNC_StaffPlan_Load(object sender,EventArgs e)
        {
            //DataTable dt = Lamex.Base.BaseBuildSql.GetDataTableBySql(@" select Account from Sys_MyUser WHERE DeptName='NC' ");
            //cmbStaff.UcDisplayMember = "Account";
            //cmbStaff.UcValueMember = "UserName";
            //cmbStaff.UcTableName = "Sys_MyUser";
            //cmbStaff.InitItme(" and DeptName='NC'");
            //cmbStaff.DataSource = dt;
            //if(dt.Rows.Count!=0)
            //{
            //    cmbStaff.Properties.Items.Clear();
            //    foreach (DataRow dr in dt.Rows)
            //    {
            //        cmbStaff.Properties.Items.Add(dr["Account"].ToString());
            //    }
            //}
            refreshData();
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            //if(string.IsNullOrEmpty(cmbStaff.Text))
            //{
            //    XtraMessageBox.Show("人员帐号不能空！");
            //    return;
            //}
            //if (string.IsNullOrEmpty(txtRate.Text))
            //{
            //    XtraMessageBox.Show("人员系数不能为空！");
            //    return;
            //}
            //DataTable dtExist = BaseBuildSql.GetDataTableBySql(@" select Account from Sys_MyUser where Account='" + cmbStaff.Text + "' and DeptName='NC'");
            //if (dtExist.Rows.Count==0)
            //{
            //    XtraMessageBox.Show("NC不存在该人员帐号");
            //    return;
            //}
            try
            {
                bool IsBool =false;
                BaseBuildSql.ExecuteNonQuery("delete from NC_Convert_StaffPlan");
                for (int i = 0; i < gridView1.RowCount; i++)
                {
                    DataTable dtRecordExist = BaseBuildSql.GetDataTableBySql(@" select Account from NC_Convert_StaffPlan where Account='" + gridView1.GetRowCellDisplayText(i, "Account").ToString() + "'");
                    if (dtRecordExist.Rows.Count != 0)
                    {
                        XtraMessageBox.Show("该人员已经分配过！");
                        return;
                    }

                    NC_PN_StaffPlanInfo model = new NC_PN_StaffPlanInfo();
                    model.Account = gridView1.GetRowCellDisplayText(i, "Account").ToString();
                    model.UserName = gridView1.GetRowCellDisplayText(i, "UserName").ToString();
                    model.Coefficient = decimal.Parse(gridView1.GetRowCellDisplayText(i, "Coefficient").ToString());
                    model.CreateBy = Loginer.CurrentUser.Account;
                    model.CreateDate = DateTime.Now;
                    IsBool = BaseFactory<NC_PN_StaffPlan>.Instance.InsertKey(model);
                }
                if(IsBool==true)
                {
                    XtraMessageBox.Show("保存成功！");
                    refreshData();
                }
                else
                {
                    XtraMessageBox.Show("保存失败！");
                }
            }catch(Exception ex)
            { XtraMessageBox.Show(ex.Message); }


        }

        private void cmbStaff_UcTextChanged(object sender, EventArgs e)
        {
            //accoutId = cmbStaff.Text;
        }
        private void refreshData()
        {
            DataTable dt = BaseBuildSql.GetDataTableBySql("select * from NC_Convert_StaffPlan");
            gcStaffPlan.DataSource= dt;
        }
        private void gcStaffPlan_Click(object sender, EventArgs e)
        {
            if (gridView1.RowCount <= 0) return;
            int m = gridView1.FocusedRowHandle;
            iFocusRow = m;
            accoutId = gridView1.GetRowCellValue(m, "Account").ToString();
            //cmbStaff.Text = accoutId;
            //txtRate.Text= gridView1.GetRowCellValue(m, "Coefficient").ToString();



        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (DialogResult.Yes == XtraMessageBox.Show("【是否要删除该帐号："+ accoutId + "】", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Warning))
            {
                if (string.IsNullOrEmpty(accoutId))
                {
                    XtraMessageBox.Show("要删除人员帐号不能为空！");
                    return;
                }
                int m = BaseBuildSql.ExecuteNonQuery(@"delete from NC_PN_StaffPlan where Account='" + accoutId + "'");
                if (m > 0)
                {
                    XtraMessageBox.Show("删除成功！");
                    refreshData();
                }
                else
                {
                    XtraMessageBox.Show("删除失败！");
                }
                
            }
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            //if (DialogResult.Yes == XtraMessageBox.Show("【是否要修改该数据："+cmbStaff.Text+"】", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Warning))
            //{
            //    //if (string.IsNullOrEmpty(cmbStaff.Text))
            //    //{
            //    //    XtraMessageBox.Show("要修改人员帐号不能为空！");
            //    //    return;
            //    //}
            //    //if (string.IsNullOrEmpty(txtRate.Text))
            //    //{
            //    //    XtraMessageBox.Show("要修改的系统不能为空！");
            //    //    return;
            //    //}

            //    int m = BaseBuildSql.ExecuteNonQuery(@"update NC_PN_StaffPlan set Account='"+cmbStaff.Text+ "',Coefficient='"+txtRate.Text+"' where Account='" + accoutId + "'");
            //    if (m > 0)
            //    {
            //        XtraMessageBox.Show("修改成功！");
            //        refreshData();
            //    }
            //    else
            //    {
            //        XtraMessageBox.Show("修改失败！");
            //    }
            //}
        }

        private void gridView1_KeyPress(object sender, KeyPressEventArgs e)
        {
            int m = gridView1.FocusedRowHandle;
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            gridView1.AddNewRow();
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            if (iFocusRow<0) return;
            iFocusRow = gridView1.FocusedRowHandle;
            if (DialogResult.Yes == XtraMessageBox.Show("是否要删除帐号："+ gridView1.GetRowCellValue(iFocusRow, "Account").ToString()+ "！", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning))
            {
                string userName = gridView1.GetRowCellValue(iFocusRow, "CreateBy").ToString();
                if(string.IsNullOrEmpty(userName))
                    gridView1.DeleteRow(iFocusRow);
                else
                {
                    BaseBuildSql.ExecuteNonQuery(@"delete from NC_Convert_StaffPlan where Account='" + accoutId + "'");
                }
            }
        }


        private void gridView1_RowCellClick(object sender, RowCellClickEventArgs e)
        {
            iFocusRow = e.RowHandle;
        }

        private void gcStaffPlan_EditorKeyPress(object sender, KeyPressEventArgs e)
        {
            GridControl view = sender as GridControl;
            int m = gridView1.FocusedRowHandle;
            string s = e.KeyChar.ToString();
            if (gridView1.FocusedColumn.FieldName == "Coefficient")
            {

                if (!char.IsDigit(e.KeyChar) && e.KeyChar != '\b' && e.KeyChar != '.' && e.KeyChar == 13)
                {
                    e.Handled = true;  //非以上键则禁止输入
                }



            }
        }

        private void gridView1_MouseEnter(object sender, EventArgs e)
        {
            iFocusRow = gridView1.FocusedRowHandle;
        }

        private void gridView1_RowClick(object sender, RowClickEventArgs e)
        {
            iFocusRow = e.RowHandle;
        }

        private void gridView1_Click(object sender, EventArgs e)
        {
            iFocusRow = gridView1.FocusedRowHandle;
        }
    }
}
